package com.dzsw.javabase.encryption;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTHeader;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.signers.JWTSigner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Base64Sample {

    public static void main(String[] args) {
//        test0();
        System.out.println(test2(12, 12));
        System.out.println(test2(12, 24));
        System.out.println(test2(12, 36));
        System.out.println(test2(12, 48));
        System.out.println(" ===================================================== ");
        System.out.println(test3(12, 12));
        System.out.println(test3(12, 24));
        System.out.println(test3(12, 36));
        System.out.println(test3(12, 48));
        System.out.println(" ===================================================== ");
        for (int i = 0; i < 301; i++) {
            System.out.println(i + " --- " + test3(12, i));
        }
    }

    public static void test0() {
        String sql = "INSERT INTO NECLIT.INVOICE_I (KOKYAKU_CD, NO, IMAGE_KBN, PAGE, LINE, ROWTYPE, ARTICLEROWID, DETAIL) VALUES ('0000', 'ZXK0003', 1, 1, ";
        String sql0 = ", 5, 2, '       GPD-WIN-MIN-2-";
        String sql1 = "                                                                             ');";
        for (int i = 0; i < 60; i++) {
            String finalSql = sql + (33 + i) + sql0 + (11 + i) + sql1;
            System.out.println(finalSql);
        }
    }

    public static void test1() {
        String str = "";
        String[] arr = str.split("\\.");
        for (String s : arr) {
            System.out.println(new String(Base64.decode(s)));
        }
        JWT jwt = JWT.of(str);
        JWTPayload payload = jwt.getPayload();
        JWTHeader header = jwt.getHeader();
        JWTSigner signer = jwt.getSigner();
        Map<String, Object> map = BeanUtil.beanToMap(jwt.getPayload());
        System.out.println(JSONUtil.toJsonStr(map));
    }

    public static String test2(int size, int rowId) {
        Integer pageNum = size;
        Integer[] pageLine = new Integer[2];
        Integer page, line;

        if (rowId <= pageNum) {
            pageLine[0] = 1;
            pageLine[1] = rowId;
        } else {
            page = (rowId / pageNum) + 1;
            line = rowId % pageNum;
            if (line == 0) {
                line = pageNum;
            }
            pageLine[0] = page;
            pageLine[1] = line;
        }
        return Arrays.stream(pageLine).map(String::valueOf).collect(Collectors.joining(","));
    }

    public static String test3(int size, int rowId) {
        List<Integer> list = doTest3(size, rowId);
        return list.stream().map(String::valueOf).collect(Collectors.joining(","));
    }

    public static List<Integer> doTest3(int size, int rowId) {
        List<Integer> retList = new ArrayList<>();
        if (rowId == 0) {
            retList.add(0);
            retList.add(0);
        } else {
            Integer remainder = rowId % size;
            Integer page = rowId / size + (remainder == 0 ? 0 : 1);
            retList.add(page);
            retList.add(remainder == 0 ? size : remainder);
        }
        return retList;
    }

}
